package ua.org.nuos.sdms.clientgui.client.handlers;

import com.vaadin.ui.ComponentContainer;
import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.InfoMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.SearchGroupEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.SearchGroupListener;
import ua.org.nuos.sdms.clientgui.client.view.selectGroup.SearchGroupComponent;
import ua.org.nuos.sdms.clientgui.client.view.selectGroup.SearchResultComponent;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.vo.GroupSearch;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 08.03.12
 * Time: 16:06
 * To change this template use File | Settings | File Templates.
 */
public class SearchGroupHandler implements SearchGroupListener {
    @Override
    public void search(SearchGroupEvent event) {
        try {
            GroupSearch search = AppProxy.getInstance().getModelLocator().getGroupSearch();
            List<Group> list = AppProxy.getInstance().getServiceLocator().getClientBean().findGroup(search);
            if (list.isEmpty()) {
                AppProxy.getInstance().getController(GlobalBoard.ID).fire(new InfoMessageEvent(Messages.NO_GROUP_FOUND_MESSAGE));
            } else {
                ComponentContainer parent = AppProxy.getInstance().getComponentLocator().getContainer(SearchGroupComponent.ID);
                parent.removeAllComponents();
                parent.addComponent(new SearchResultComponent(list));
            }
        } catch (Exception e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Cant findByEmail group", e);
            AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
        }
    }
}
